﻿using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class GetDeepProcessingHandler : IRequestHandler<GetDeepProcessingCommand, ApiResult<DeepProcessingDTO>>
    {
        private readonly IBase<DeepProcessingModel> deepProcessingRepo;

        public GetDeepProcessingHandler(IBase<DeepProcessingModel> deepProcessingRepo)
        {
            this.deepProcessingRepo = deepProcessingRepo;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<DeepProcessingDTO>> Handle(GetDeepProcessingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<DeepProcessingDTO> result = new ApiResult<DeepProcessingDTO>();
            var deepProcessing = deepProcessingRepo.GetAll().Where(x => !x.IsDeleted);
            if (!string.IsNullOrEmpty(request.Batch))
            {
                deepProcessing = deepProcessing.Where(x=>x.Batch.Contains(request.Batch));
            }
            var linq = (from a in deepProcessing
                        select new DeepProcessingDTO
                        {
                           Batch= a.Batch,
                           DeepProcessingId=a.DeepProcessingId,
                           Manager=a.Manager,
                           WorkTeam=a.WorkTeam
                        }).FirstOrDefault(x=>x.Batch==request.Batch);
            result.Data = linq;
            result.Code = ApiEnum.OK;
            result.Msg = "深加工查询列表成功";
            return Task.FromResult(result);
        }
    }
}
